package com.leilbase.userservice.controllers;

import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.leilbase.userservice.feign.AdminServiceLbs;
import com.leilbase.userservice.services.UserDemoSerivce;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserDemoController {

    static Logger logger = LoggerFactory.getLogger(UserDemoController.class);

    @Autowired
    UserDemoSerivce userDemoSerivce;

//    @Autowired
//    AdminServiceLbs adminServiceLbs;

    @RequestMapping(name = "/hiRibbon", method = RequestMethod.GET)
    @SentinelResource(value = "hiRibbon", blockHandler = "exceptionHandler", fallback = "fallbackHandler")
    public String hiRibbon() {
//        try {
//            return this.userDemoSerivce.testRibbon();
//        } catch (IllegalStateException e) {
//            logger.error(e.getMessage());
//            return e.getMessage();
//        }
        return this.userDemoSerivce.testRibbon();
    }

//    @RequestMapping(name = "/hiFeign", method = RequestMethod.GET)
//    public String hiFeign() {
//        return adminServiceLbs.adminSayHi();
//    }

    public String exceptionHandler(BlockException ex) {
        logger.error("访问超出了限流规则");
        return "限流警报";
    }


    public String fallbackHandler() {
        logger.error("访问出现了错误");
        return "服务器报错了";
    }


}
